$(function () {
    //加载表格数据
    methods.getTableData();
});

var data = {
    //页面对话框
    dialog: {
        show: false,
        loading: false,
        disabled: false,
        title: "信息"
    },
    //分页
    paging: {
        currentPage: 1,
        pageSize: 10,
        total: 11,
        tableData: []
    },
    //搜索
    search: {},
    //当前对象
    currentObj: {},
    //角色列表
    roleList: [],
    //部门列表
    deptList: [],
    //选择的角色
    roles: [],
    //选择的部门
    depts: [],
};

var methods = {
    //搜索用户
    searchUser: function () {
        data.paging.currentPage = 1;
        methods.getTableData();
    },
    //更新用户对话框
    updateUserDialog: function (row) {
        data.dialog.title = "修改【" + row.nickname + "】用户";
        data.dialog.disabled = true;
        data.currentObj = JSON.parse(JSON.stringify(row));
        data.roles = row.roleIds ? row.roleIds.split(",") : [];
        data.depts = row.deptIds ? row.deptIds.split(",") : [];
        data.dialog.show = true;
    },
    //获取所有角色
    getAllRole: function () {
        ajax.post("/system/user/getAllRole", function (res) {
            data.roleList = res.data;
        })
    },
    //获取所有的部门
    getAllDept: function () {
        ajax.post("/system/dept/getAllDept", function (res) {
            console.log(res);
            data.deptList = res.data;
        })
    },
    //删除用户
    deleteUser: function (row) {
        layer.confirm("您确定要删除【" + row.nickname + "】用户吗？", function (index) {
            layer.close(index);
            ajax.post("/system/user/deleteUser", {
                id: row.id
            }, function () {
                layer.msg("删除成功");
                methods.getTableData();
            })
        })
    },
    //更新或保存用户
    saveOrUpdateUser: function () {
        data.dialog.loading = true;
        data.currentObj.roleIds = data.roles.join(",");
        data.currentObj.deptIds = data.depts.join(",");
        ajax.post("/system/user/saveOrUpdateUser", data.currentObj, function (res) {
            data.dialog.loading = false;
            if (res.code == 1) {
                layer.msg(res.msg);
                data.dialog.show = false;
                methods.getTableData();
            } else {
                layer.alert(res.msg);
            }
        });
    },
    //加载数据
    getTableData: function () {
        var load = layer.load(2);
        ajax.post("/system/user/getUserPagingData", {
            roleId: data.search.roleId,
            sContent: data.search.sContent,
            currentPage: data.paging.currentPage,
            pageSize: data.paging.pageSize
        }, function (res) {
            layer.close(load);
            var tableData = res.data.rows;
            //处理角色名称
            for (var i = 0; i < tableData.length; i++) {
                var row = tableData[i];
                var roleNames = [];
                if (row.roleIds) {
                    var roles = row.roleIds.split(",");
                    for (var j = 0; j < roles.length; j++) {
                        roleNames.push(methods.getRoleName(roles[j]))
                    }
                }
                row.roleNames = roleNames;
                //处理部门名称
                var deptNames = [];
                if (row.deptIds) {
                    var depts = row.deptIds.split(",");
                    for (var j = 0; j < depts.length; j++) {
                        var deptName = methods.getDeptName(depts[j]);
                        deptNames.push(deptName);
                    }
                }
                row.deptNames = deptNames;
            }
            console.log(tableData);
            data.paging.tableData = tableData;
            data.paging.total = res.data.total;
        })
    },
    //获取角色名称
    getRoleName: function (id) {
        for (var i = 0; i < data.roleList.length; i++) {
            var role = data.roleList[i];
            if (role.id == id) {
                return role.name;
            }
        }
    },
    getDeptName: function (id) {
        for (var i = 0; i < data.deptList.length; i++) {
            var dept = data.deptList[i];
            if (dept.id == id) {
                return dept.name;
            }
        }
    },
    //处理页面大小变化
    handleSizeChange: function (val) {
        data.paging.pageSize = val;
        data.paging.currentPage = 1;
        methods.getTableData();
    },
    //处理当前页面变化
    handleCurrentChange: function (val) {
        data.paging.currentPage = val;
        methods.getTableData();
    },
    handleDialogClose: function () {
        data.dialog.loading = false;
        data.dialog.disabled = false;
        data.roles = [];
        data.depts = [];
        data.currentObj = {};
    }

};
var vm = new Vue({
    data: data, methods: methods, created: function () {
        methods.getAllRole();
        methods.getAllDept();
    }
}).$mount("#app");
